﻿using DFGK.WMS.Common.Tools;
using DFGK.WMS.Common.Web;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;
using System.Threading.Tasks;

namespace DFGK_WMS.WebAPI.Controllers.设备
{
    /// <summary>
    /// 输送线
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PipeLineController : ControllerBase
    {
        public ITPutawayOrderRepository TPutawayOrderService { get; set; }

        public ITCellRepository TCellRepository { get; set; }
        public PipeLineController(ITPutawayOrderRepository tPutawayOrderRepository, ITCellRepository tCellRepository)
        {
            TPutawayOrderService = tPutawayOrderRepository;
            TCellRepository = tCellRepository;
        }
        /// <summary>
        /// 输送线出入库任务状态上报
        /// </summary>
        /// <param name="putdto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult TaskAppear(PipeLineDto putdto)
        {
            if (putdto.method != "end")
            {
                return Ok(BaseResult.ReturnInfo(1, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            var order = TPutawayOrderService.Getputawayorder(x=>x.Code==putdto.taskCode);
            if (order == null)
            {
                return Ok(BaseResult.ReturnInfo(1, "未能找到任务", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            //输送线任务完成
            if (putdto.method == "end")
            {
                //入库任务
                if (order.SourceType == 1)
                {
                    //堆垛机入库
                    //var data = new { taskCode = order.Code, taskType = order.SourceType, startPoint = putdto.roadwayCode, endPoint = order.LocationCode };
                    TaskLiku data = new TaskLiku()
                    {
                        taskId = order.Code,
                        taskSubType = order.SourceType.ToString(),
                        startPositionId=putdto.roadwayCode,
                        endPositionId = order.LocationCode,
                        palletCode=order.PalletBarcode
                    };
                    var wcsdata= HttpHelper.WCSSendPostRequest("Api/ApiWCS/ReceiveTask", data);
                    LogHelper.Info("agv:" + wcsdata);

                }
                //出库任务
                else if (order.SourceType == 2)
                {
                    //仓位查询
                    var cell = TCellRepository.Gettcell(order.LocationCode);
                    if (cell == null)
                    {
                        return Ok(BaseResult.ReturnInfo(1, "未找到该仓位", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    //修改仓位
                    cell.FullStatus = 0;
                    if (!TCellRepository.UpdateCell(cell))
                    {
                        return Ok(BaseResult.ReturnInfo(1, "仓位修改失败", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    //调用库存接口
                    //任务修改
                    order.Status = 5;
                    if (!TPutawayOrderService.UpdatePutawayorder(order))
                    {
                        return Ok(BaseResult.ReturnInfo(1, "任务修改失败", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    }
                    if (order.SiteCode=="")
                    {
                        //做判断调用agv接口
                        var date = new { TaskID = order.Code, materialcode = order.MaterialCode, Palletcode = order.PalletBarcode, Fromside = order.SiteCode, Agvgroup = "Agvgroup", Priority = 1 };
                        var agvdt = HttpHelper.AgvSendPostRequest("script-api/getTaskByWes", date);
                        LogHelper.Info("agv:" + agvdt);
                    }
                    return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                }
            }
            return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
        /// <summary>
        /// 输送线入库任务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetPutTaskData(PutLineDto dto)
        {
            var order = TPutawayOrderService.Getputawayorder(x => x.PalletBarcode == dto.barCode);
            if (order == null)
            {
                return Ok(BaseResult.ReturnInfo(1, "未能找到任务", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            }
            var data = new { taskCode = order.Code, taskType = order.SourceType, barCode = order.PalletBarcode, startPoint=order.SiteCode, endPoint=order.LocationCode };
            return Ok(BaseResult.ReturnInfo(0, "",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),data));
        }
        /// <summary>
        /// 入库拣选
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PutChooseTask(PutChooseDto dto)
        {
            var data=HttpHelper.SendPostRequest("入库拣选", dto);
            LogHelper.Info("入库拣选："+data);
            return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
        /// <summary>
        /// 出库任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult TaskOutData(TaskOutDto dto )
        {
            var data = HttpHelper.SendPostRequest("出库任务", dto);
            LogHelper.Info("出库任务：" + data);
            return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }

        /// <summary>
        /// 获取当前任务巷道节点
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetRoadwayCode(RoadwayCode code)
        {
            var data = HttpHelper.SendPostRequestes("", code);
            LogHelper.Info("巷道节点" + data);
            return Ok(BaseResult.ReturnInfo(0, "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }
    }
}
